#!/bin/bash

xdefine_first ( )
{
	echo "xdefine_first"
	#txtfile=`ls $PATHIMLIS`
	txtfile_pwd=`echo $PATHIMLIS`
	sT=`date -u +%Y%m%d`
	#sT="20240311"
	#Newimgtxtfile=`echo $sT | awk '{print("imlis_"$1".txt")}'`
	Newimglist=`echo $txtfile_pwd $sT | awk '{print($1"imlis_"$2".txt")}'`
#	Newimglist="/home/cgft/software/softgft_mainFocus/xgftOnline.sh/imlis_20221204.txt.bak"
	echo $Newimgtxtfile
	echo $Newimglist	
	#Newimglist=`echo "$HOME/data/history/gft_observed.list"`
	#Newimglistbak=`echo "$HOME/data//history/Yyyyy/gft_observed_yymmdd.list"`

	stringtimeForMonitorT=`date -u +%Y%m%d`
	dir_reduction=/disk2/test/xlp2/svom/$stringtimeForMonitorT
	cd /disk2/test/xlp2/svom
	if test ! -d $stringtimeForMonitorT
	then		
		echo "making $stringtimeForMonitorT"
		mkdir $stringtimeForMonitorT
    fi
	cd $dir_reduction
	#xcheck_If_process_old_image
	if test -s $Newimglist
	then
	  cat $Newimglist | column -t | awk '{print($1"/"$2)}' >oldlist
	fi
}


xcheck_If_process_old_image ( )
{
  echo "xcheck_If_process_old_image"
  #echo "Reduce the fits in the history list [ Y/N ]:"
	#read AnswerWords
	#AnswerWords='N'
	#if [ "$AnswerWords"x == "Y"x ]
	#then
	#    echo "OK, these fit images will be reduced one by one"
	#    sleep 1
	#else
	#    cat $Newimglist | column -t | awk '{print($1"/"$2)}' >oldlist
	#
	#fi
}

xdefine ( )
{
	echo "new loop"
	#txtfile=`ls $PATHIMLIS`
	txtfile_pwd=`echo $PATHIMLIS`
	sT=`date -u +%Y%m%d`
	#sT="20240311"
	Newimgtxtfile=`echo $sT | awk '{print("imlis_"$1".txt")}'`
	Newimglist=`echo $txtfile_pwd $sT | awk '{print($1"imlis_"$2".txt")}'`

	stringtimeForMonitorT=`date -u +%Y%m%d`
	dir_reduction=/disk2/test/xlp2/svom/$stringtimeForMonitorT
	cd /disk2/test/xlp2/svom
	if test ! -d $stringtimeForMonitorT
	then		
		echo "making $stringtimeForMonitorT"
		mkdir $stringtimeForMonitorT
    fi
    cd $dir_reduction

}


#echo "bias dark flat focus light" | xargs -n 1 >listdatafile 


xobjectreduction (  )
{
    fitname=$1
    echo $fitname
    #xflatcheckandcombine 
    #sh /home/cgft/software/softgft_mainFocus/xgft_ot.sh $fitname
    sh /home/cgft/software/softgft_mainFocus/xgft_ot_LocalPs1_usno.sh $fitname
    #sh /home/cgft/software/softgft/xgft_ot.sh $fitname
}
xbiascheckandcombine (  )
{
    :
}
xdarkcheckandcombine (  )
{
    :
}
xflatcheckandcombine (  )
{
    dir_now=`pwd`
    dirFlat="/disk2/test/xlp2/svom/caliFile/Flat"
    #dirFlatg="/disk2/test/xlp2/svom/caliFile/Flat_g.fit"
    #dirFlati="/disk2/test/xlp2/svom/caliFile/Flat_i.fit"
    dirZero="/disk2/test/xlp2/svom/caliFile/Zero.fit"
    filter=`gethead $fitname "filter"`
    flatnamedir=`echo $dirFlat"_"$filter".fit"`
    if [ -r $flatnamedir ] && [ -r $dirZero  ]
    then
	    cp $flatnamedir $dirZero ./
	    flatname=`echo "Flat_"$filter".fit"`
	    xzeroflatcorr.sh $fitname $flatname
    fi
}

xcopyobslist (  )
{
   # echo "xcopyobslist"
    if test ! -r oldlist
    then
        touch oldlist
    fi
    if test -s $Newimglist 
    then
	echo "have $Newimglist"
        cp $Newimglist $dir_reduction
        # gft_observed.list is copied
        #cat gft_observed.list | awk '{print($1"/"$2)}' >newlist
        cat $Newimgtxtfile | column -t | awk '{print($1"/"$2)}' >newlist
        diff oldlist newlist | grep ">" | tr -d '>' | column -t  >listmatch
        linedata=`cat listmatch | wc -l`
        if [ "$linedata" -ne 0  ]
        then
            echo "new image exits"
            cat listmatch | while read linedataroad
            do
                N_dataline=`echo $linedataroad | wc | awk '{print($2)}'`
                echo $linedataroad
                echo $N_dataline
                if [ $N_dataline -lt 1   ]
                then
                    echo "The information of this fits is not correct"
                else
                   # pathroad=`echo $linedataroad | awk '{print($1)}'`
                   # fitsname=`echo $linedataroad | awk '{print($2)}'`
                   # cp $pathroad"/"$fitsname $dir_reduction
			cp $linedataroad $dir_reduction
			fitsname=`echo $linedataroad | sed 's/\// /g' | awk '{print($NF)}'`
			echo $linedataroad >>oldlist
			echo "fitsname="$fitsname
                    #echo "$pathroad $fitsname" | column -t   >>oldlist
		    gethead $fitsname "TRIG_RA" >tmp
		    if test -s tmp
		    then
		                  echo "it is an alert image"
                    	xobjectreduction  $fitsname
        else
            echo "it is not an alert image, give up and wait for..."
        fi

		fi
            done
        else
            echo "No new image exist"
            sleep 1
        fi 
    else
        #echo "No any data"
        sleep 1
        #continue
    fi

}

xcheckdatafile (  )
{

    xcopyobslist


    #for datafilename in `cat listotnamefile`
    #do
    #   Ab_datafilename=`echo $Dir_rawdata"/"$datafilename`
    #   echo $Ab_datafilename
    #   cd $Ab_datafilename
    #   xchecknewimage
    #done

}

xdefine_first

while :
do
    xdefine
    cd $dir_reduction
    xcopyobslist
    #xcheckdatafile
    sleep 1
done
